package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Question;
import com.zzty.txj1.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @RequestMapping("/insertQuestion/{content}")
    public Map addQuestion(@PathVariable("content") String content){
        System.out.println("insertQuestion content: "+content);
        Map<String,Integer> map=new HashMap<>();
        Integer count=questionService.addQuestion(content);
        if(count==1){
            map.put("count",1);
        }else{
            map.put("count",0);
        }
        return map;
    }

    @RequestMapping("/getAllQuestion")
    public Map<String,List<Question>> getAllQuestion(){
        Map<String,List<Question>> map=new HashMap();
        List<Question> questions=questionService.getAllQuestion();
        if(questions!=null||!questions.isEmpty()){
            System.out.println("getAllQuestion questions: "+questions.toString());
            map.put("questions",questions);
        }else{
            map.put("questions",null);
        }
        return map;
    }

    @RequestMapping("/getQuestionById/{questionId}")
    public Map<String,Question> getQuestionById(@PathVariable("questionId") Integer questionId){
        Map<String,Question> map=new HashMap<>();
        Question question=questionService.getQuestionById(questionId);
        if(question!=null){
            map.put("question",question);
        }else{
            map.put("question",null);
        }
        return map;
    }

    @RequestMapping(value = "/addContent",method = RequestMethod.POST)
    public Question addContent(Question question){
        Question question1=questionService.addContent(question);
        return question1;
    }

    @RequestMapping(value = "/getAllQuestions")
    public List getAllQuestions(){
        List<Question> questions=questionService.getAllQuestion();
        return questions;
    }

    @RequestMapping("/getNewQuestion")
    public Question getNewQuestion(){
        Integer questionId=questionService.getMaxQuestionId();
        Question question=questionService.getQuestionById(questionId);
        System.out.println("maxQuestionId== "+questionId+" "+"question=== "+question);
        return question;
    }

}
